<?php
	include '../init.php';
	$a=$_GET['a'];
	$model=new Model('category');

	switch ($a) 
	{
		case 'add':
			//判断是否为空
			if (empty($_POST['name'])) 
			{
				error('请输入类名');
				return false;
			}

			$data['name']=$_POST['name'];
			$data['pid']=$_POST['pid'];
			$data['path']=$_POST['path'];
			$data['addtime']=time();

			//插入数据
			$res=$model->insert($data);
			if ($res) 
			{
				success('插入类名成功',ADMIN_URL.'/category/index.php',2);
				return false;
			}
			break;
		
		case 'up':
			//接受当前页
			$p=$_GET['p'];

			$where['id']=$_GET['id'];
			$status=$model->select($where);

			$data['status']=$status[0]['status']==0?1:0;
			$pid=$status[0]['pid'];

			$res=$model->update($data,$where);
			if ($res) 
			{
				header('location:'.ADMIN_URL.'/category/index.php?pid='.$pid.'&p='.$p);
			}
			break;

			case 'del':
				$id=$_GET['id'];
				$sql="select * from `category` where pid=$id";

				$res=$model->find($sql);

				if (!empty($res)) 
				{
					error('不能直接删除类名！');
					return false;
				}else{
					$res1=$model->delete($id);
					success('删除成功',ADMIN_URL.'/category/index.php',1);
					return false;
				}
				break;
	}

?>